package com.lanxin.judge.controller;


import com.lanxin.hr.entity.ZzDepartment;
import com.lanxin.hr.service.IZzDepartmentService;
import com.lanxin.judge.Vo.NodeVo;
import com.lanxin.judge.entity.LcNode;
import com.lanxin.judge.service.ILcNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-08-04
 */
@Controller
@RequestMapping("judge/node")
public class LcNodeController {

    @Autowired
    private ILcNodeService nodeService;

    @Autowired
    private IZzDepartmentService departmentService;

    //获取流程的审批节点
    @RequestMapping("/listNode")
    public ModelAndView listNode(@RequestParam Integer flowId){
        ModelAndView modelAndView = new ModelAndView();
        //获取排序后的流程节点,从前往后
        List<NodeVo> nodeVos = nodeService.listNode(flowId);
        //获得所有部门
        List<ZzDepartment> departments = departmentService.listDepartment();
        modelAndView.addObject("flowId", flowId);
        modelAndView.addObject("nodeVos", nodeVos);
        modelAndView.setViewName("judge/listNode");
        return modelAndView;
    }

    //删除节点
    @RequestMapping("/deleteNode")
    @ResponseBody
    public boolean deleteNode(@RequestParam Integer oid,@RequestParam Integer flowId){
        try {
            nodeService.deleteNode(oid,flowId);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    //编辑节点，包括修改和添加节点
    @RequestMapping("/editNode")
    public String editNode(LcNode node){
        /*添加节点-begin，如果node的oid为空则为添加节点*/
        if(node.getOid() == null){
            //node.getParentid()为流程id
            try {
                nodeService.addNode(node.getParentid(),node);
            }catch (Exception e){
                e.printStackTrace();
                return "redirect:listNode?flowId="+node.getParentid();
            }
            /*添加节点-end*/
        }else {
            /*修改节点-begin*/
            try {
                nodeService.updateNode(node);
            }catch (Exception e){
                e.printStackTrace();
            }
            /*修改节点-end*/
        }
        return "redirect:listNode?flowId="+node.getParentid();
    }
}
